package dao

import (
	"context"
	"fmt"
	"shopAdd-srv/global"
	"shopAdd-srv/internal/model"
)

type ShopServiceImpl interface {
	ShopAdd(shop model.Shop) (uint, error)
}
type ShopService struct {
}

func NewShopService() *ShopService {
	return &ShopService{}
}
func (s *ShopService) ShopAdd(shop model.Shop) (uint, error) {

	var shopAdd model.Shop
	shopAdd = model.Shop{
		Name:   shop.Name,
		Price:  shop.Price,
		Number: shop.Number,
		UserId: shop.UserId,
	}
	err := shopAdd.ShopAdd(global.DB)
	if err != nil {
		return 0, err
	}

	err = RedisStock(int(shopAdd.ID), shopAdd.Number)
	if err != nil {
		return 0, err
	}

	return shopAdd.ID, nil
}

func RedisStock(shopId int, number int) error {

	key := fmt.Sprintf("shopId:%d", shopId)

	for i := 1; i <= number; i++ {
		err := global.Rdb.LPush(context.Background(), key, i).Err()
		if err != nil {
			return err
		}
	}
	return nil

}
